package com.crhms.seabow.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crhms.seabow.entity.Category;
import com.crhms.seabow.entity.Lesson;
import com.crhms.seabow.model.CateLessonResult;
import com.crhms.seabow.repository.CategoryRepository;
import com.crhms.seabow.repository.LessonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hkk
 */
@Service
public class CategoryService {

    @Autowired
    private LessonRepository lessonRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    public List<CateLessonResult> getCateLessons(String bookId) {

        List<Category> categories = categoryRepository.selectList(null);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("book_id",bookId);
        List<Lesson> lessons = lessonRepository.selectList(wrapper);

        List<CateLessonResult> result = new ArrayList<>();
        categories.stream().forEach(c->{
            CateLessonResult cateLesson = new CateLessonResult();
            cateLesson.setCode(String.valueOf(c.getId()));
            cateLesson.setName(c.getName());
            cateLesson.setLessons(lessons);
            result.add(cateLesson);
        });

        return  result;
    }
}
